<template>
  <div class="dashboard-container">
    <div class="app-container">
      <upload-excel :before-upload="beforeUpload" :on-success="handleSuccess" />
    </div>
  </div>
</template>

<script>
import employee from '@/constant/employee'
const { mapKeyPath } = employee
import { parseTime } from '@/utils'
import { importEmployee } from '@/api/employees'
export default {
  methods: {
    beforeUpload(file) {
      if (file.name.endsWith('.xlsx') || file.name.endsWith('.xls')) {
        return true
      }
      return alert('请选择excel文件')
    },
    async handleSuccess({ header, results }) {
      const data = results.map(item => {
        const obj = {}
        for (const key in mapKeyPath) {
          if (key === '入职日期' || key === '转正日期') {
            const timesteap = item[key]
            const date = new Date((timesteap - 1) * 24 * 3600000)
            const time = date.setFullYear(date.getFullYear() - 70)
            console.log(time)
            obj[mapKeyPath[key]] = parseTime(time)
          } else {
            obj[mapKeyPath[key]] = item[key]
          }
        }
        console.log(obj)
        return obj
      })
      await importEmployee(data)
      this.$router.go(-1)
      this.$message.success('批量导入成功')
    }
  }
}
</script>

<style>

</style>
